package org.tio.utils.lock;

import java.util.Map;
import java.util.WeakHashMap;

/**
 * 锁对象工具类
 */
public class LockUtils {

	private final static Map<String, Object> LOCAL_LOCKS = new WeakHashMap<>();

	/**
	 * 获取锁对象，用于synchronized(lockObj)
	 * @param key
	 * @return
	 * @author tanyaowu
	 */
	public static Object getLockObj(String key) {
		Object lock = LOCAL_LOCKS.get(key);
		if (lock == null) {
			synchronized (LockUtils.class) {
				lock = LOCAL_LOCKS.get(key);
				if (lock == null) {
					lock = new Object();
					LOCAL_LOCKS.put(key, lock);
				}
			}
		}
		return lock;
	}

}
